<?php

class Ext_Attachment_Video_Add
{
    protected $_articleId;

    protected $_messager;
    const VIDEO_EXISTS              = 'videoExists';
    protected $_messageTemplates = array(
        self::VIDEO_EXISTS              => "Video '%name%' already exists.",
    );
    
    public function __construct($articleId)
    {
        $this->_articleId = $articleId;
        $this->_messager  = new Ext_Messager($this->_messageTemplates);
    }

    public function add($data)
    {
        $result = array(
            'code'     => Ext_Attachment_Video_Add_Result::FAILURE,
            'data'     => '',
            'messages' => array(),
        );
        
        $videoIdArray = array();
        $articleVideo = App_Model_DbTable_Article_Attachment_Video::getInstance()->getRowByArticleId($this->_articleId);
        if($articleVideo){
            $ids = $articleVideo['ids'];

            $idsArray = explode(';', $ids);
            for($i=0;$i<count($idsArray);$i++)
            {
                $attachmentVideoId = $idsArray[$i];
                if(!$attachmentVideoId){
                    continue;
                }

                $attachmentVideo = App_Model_DbTable_Attachment_Video::getInstance()->getRowById($attachmentVideoId);
                if(!$attachmentVideo){
                    continue;
                }
                $videoIdArray[] = $attachmentVideo['videoId'];
            }
        }
        
        $videoId = $data->videoId;
        if(in_array($videoId, $videoIdArray)){
             $result['messages'][] = $this->_messager->addMessage(self::VIDEO_EXISTS, array('name'=>$data->title));
             return new Ext_Attachment_Video_Add_Result($result['code'], $result['data'], $result['messages']);
        }
        
        $row = array(
            'articleId'     => $this->_articleId,
            'videoId'       => $videoId,
            'title'         => $data->title,
        );
        $attachmentVideoId = App_Model_DbTable_Attachment_Video::getInstance()->addNew($row);
        
        $videor = new Ext_Article_Attachment_Video($this->_articleId);
        $videor->add($attachmentVideoId);
        
        $article = App_Model_DbTable_Article::getInstance()->getRowById($this->_articleId);
        if(!$article){
            throw new Ext_Attachment_Video_Add_Exception("Article record not found.");
        }
        
        $attachmentsArray = unserialize($article['attachments']);
        if(!is_array($attachmentsArray)){
            $attachmentsArray = array();
        }
        $attachment = array("type"=>'video', "id"=>$attachmentVideoId);
        array_unshift($attachmentsArray, $attachment);
        if($attachmentsArray){
            $row = array(
                'attachments'  => serialize($attachmentsArray),
            );
            App_Model_DbTable_Article::getInstance()->addOld($row, $this->_articleId);
        }
        
        $result['code'] = Ext_Attachment_Video_Add_Result::SUCCESS;
        $result['data'] = $attachmentVideoId;
        return new Ext_Attachment_Video_Add_Result($result['code'], $result['data'], $result['messages']);
    }
}
